//深度克隆
function deepClone(source) {
  const isArray = Array.isArray(source);
  const isObj = typeof source === "object";
  const isFun = typeof source === "function";
  const target = isArray ? [] : {};
  if (!isObj && !isFun) return source;
  for (let i in source) {
    if (source.hasOwnProperty(i)) {
      target[i] =
        typeof source[i] === "object" ? deepClone(source[i]) : source[i];
    }
  }
  return target;
}

//深度合并
function deepMerge(target = {}, source = {}) {
  target = deepClone(target);
  if (typeof target !== "object" || typeof source !== "object") return false;
  for (let prop in source) {
    if (!source.hasOwnProperty(prop)) continue;
    if (prop in target) {
      if (typeof target[prop] !== "object") {
        target[prop] = source[prop];
      } else {
        if (typeof source[prop] !== "object") {
          target[prop] = source[prop];
        } else {
          if (target[prop].concat && source[prop].concat) {
            target[prop] = target[prop].concat(source[prop]);
          } else {
            target[prop] = deepMerge(target[prop], source[prop]);
          }
        }
      }
    } else {
      target[prop] = source[prop];
    }
  }
  return target;
}

export { deepClone, deepMerge };
